﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace AQMProject
{
    class RssDAO
    {
        public void Insert(rssuebersicht rssuebersicht)
        {
            AQMDataDataContext db = new AQMDataDataContext();

            try
            {
                db.rssuebersicht.InsertOnSubmit(rssuebersicht);
                db.SubmitChanges();
            }
            catch { }               
        }

        public void Update()
        {
        }

        public void Delete(String rsslink)
        {
            using (AQMDataDataContext db = new AQMDataDataContext())
            {
                var deleteOrderDetails =
                        from c in db.aktienrssflinks
                        where c.link == rsslink
                        select c;

                foreach (var detail in deleteOrderDetails)
                {
                    db.aktienrssflinks.DeleteOnSubmit(detail);
                }

                try
                {
                    db.SubmitChanges();
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                }

            }
        }

        public List<rssuebersicht> SelectList()
        {
            using (AQMDataDataContext db = new AQMDataDataContext())
            {
                List<rssuebersicht> data = (from c in db.rssuebersicht
                                    select c).ToList();
                return data;
            }
        }

        public String getEmpfehlung(int empf_id)
        {
            using (AQMDataDataContext db = new AQMDataDataContext())
            {
                List<empfehlung> data = (from c in db.empfehlung
                                            where c.Idempfehlung == empf_id
                                            select c).ToList();
                foreach (empfehlung empf in data)
                {
                    return empf.empfehlungsname;
                }

            }
            return "";
        }

        public String getAction(int empf_id)
        {
            using (AQMDataDataContext db = new AQMDataDataContext())
            {
                List<empfehlung> data = (from c in db.empfehlung
                                         where c.Idempfehlung == empf_id
                                         select c).ToList();
                foreach (empfehlung empf in data)
                {
                    return empf.action;
                }

            }
            return "";
        }

        public Decimal getFaktor(int empf_id)
        {
            using (AQMDataDataContext db = new AQMDataDataContext())
            {
                List<empfehlung> data = (from c in db.empfehlung
                                         where c.Idempfehlung == empf_id
                                         select c).ToList();
                foreach (empfehlung empf in data)
                {
                    return Convert.ToDecimal(empf.faktor);
                }

            }
            return 1;
        }

        public DateTime getLastDateTime()
        {
            using (AQMDataDataContext db = new AQMDataDataContext())
            {
                DateTime data = (from c in db.Einlesedaten
                                                  select c.einlesedatum).Max();
                return data;
            }
        }

        public void setLastDateTime(Einlesedaten dat)
        {
            using (AQMDataDataContext db = new AQMDataDataContext())
            {
                try
                {
                    db.Einlesedaten.InsertOnSubmit(dat);
                    db.SubmitChanges();
                }
                catch { }
            }
        }
    }
}
